/**
 * @see https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/
 * @param {string} digits
 * @return {string[]}
 */
var letterCombinations = function (digits) {
  if(!digits) {
    return [];
  }
  let map = ['', 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
  let codes = digits.split('');
  let words = [];
  for (let i = 0; i < codes.length; i++) {
    words.push(map[codes[i] - 1]);
  }
  let base = words[0].split('');

  // 一个个的计算组合
  for (let i = 1; i < words.length; i++) {
    base = getGroup(base, words[i]);
  }

  console.log(base);

  function getGroup(target, source) {
    const result = []
    for (let i = 0; i < target.length; i++) {
      const ei = target[i];
      for (let j = 0; j < source.length; j++) {
        const ej = source[j];
        result.push(ei + ej);
      }
    }
    return result;
  }

  console.log(base);
};

letterCombinations('234');
letterCombinations('');